/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.program;

import java.util.LinkedList;
import java.util.List;

import net.bioteam.appweb.gwt.apps.client.StaticHolder;
import net.bioteam.appweb.gwt.apps.clientandserver.Constant;
import net.bioteam.appweb.model.Parameter;
import net.bioteam.appweb.submit.ProgramValueBean;

import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.FormItemIfFunction;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.FormItemIcon;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.UploadItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.form.fields.events.IconClickEvent;
import com.smartgwt.client.widgets.form.fields.events.IconClickHandler;

public class MyDataItem
{

	public UploadItem ui;
	public TextAreaItem fileData;
	public TextItem serverFile;
	public TextItem USAItem;
	public RadioGroupItem radioGroupItem;

	public List<FormItem> getItems()
	{
		List<FormItem> items = new LinkedList<FormItem>();
		items.add(radioGroupItem);
		items.add(ui);
		items.add(fileData);
		items.add(serverFile);
		items.add(USAItem);
		return items;
	}

	public MyDataItem(Parameter param, ProgramValueBean bean, boolean mandatory)
	{
		String title = param.getPrompt() == null ? param.getName() : param.getPrompt();
		if (mandatory)
		{
			title += ProgramForm.REQUIRED_SUFFIX;
		} else if (param.getPrecond() != null)
		{
			title += ProgramForm.CONDITIONAL_REQUIRED_SUFFIX;
		}

		init(param, bean, ProgramForm.titleWidth, mandatory, title);

	}

	public MyDataItem(Parameter param, ProgramValueBean bean, int labelWidth)
	{
		String title = param.getPrompt() == null ? param.getName() : param.getPrompt();
		boolean mandatory = false;
		if (param.isMandatory())
		{
			if (param.getPrecond() != null)
			{
				title += ProgramForm.CONDITIONAL_REQUIRED_SUFFIX;
			} else
			{
				title += ProgramForm.REQUIRED_SUFFIX;
				mandatory = true;
			}
		}

		init(param, bean, labelWidth, mandatory, title);

	}

	public MyDataItem(Parameter param, ProgramValueBean bean)
	{
		String title = param.getPrompt() == null ? param.getName() : param.getPrompt();
		boolean mandatory = false;
		if (param.isMandatory())
		{
			if (param.getPrecond() != null)
			{
				title += ProgramForm.CONDITIONAL_REQUIRED_SUFFIX;
			} else
			{
				title += ProgramForm.REQUIRED_SUFFIX;
				mandatory = true;
			}
		}

		init(param, bean, ProgramForm.titleWidth, mandatory, title);
	}

	protected void init(Parameter param, ProgramValueBean bean, int labelWidth, boolean mandatory, String title)
	{
		String name = param.getName();

		radioGroupItem = new RadioGroupItem("_temp_" + name);
		radioGroupItem.setColSpan(2);
		radioGroupItem.setCellStyle("dataradio");
		radioGroupItem.setValueMap("File", "Text", "Server File", "USA");
		radioGroupItem.setVertical(false);

		ui = new UploadItem(name);
		ui.setTitle(title);
		ui.setWidth(ProgramForm.CELL_WIDTH);
		//http://code.google.com/p/smartgwt/issues/detail?id=216
		ui.setShowIfCondition(new FormItemIfFunction()
		{

			public boolean execute(FormItem item, Object value, DynamicForm form)
			{
				String v = radioGroupItem.getValue().toString();
				if (v.equals("File"))
					return true;
				else
					return false;
			}
		});

		fileData = new TextAreaItem(name + Constant.file_data_suffix);
		fileData.setTitle(title);
		fileData.setWidth(ProgramForm.CELL_WIDTH);
		fileData.setShowIfCondition(new FormItemIfFunction()
		{

			public boolean execute(FormItem item, Object value, DynamicForm form)
			{
				String v = radioGroupItem.getValue().toString();
				if (v.equals("Text"))
					return true;
				else
					return false;
			}
		});

		USAItem = new TextItem(name + Constant.file_usa_suffix);
		USAItem.setTitle(title);
		USAItem.setWidth(ProgramForm.CELL_WIDTH);
		USAItem.setShowIfCondition(new FormItemIfFunction()
		{

			public boolean execute(FormItem item, Object value, DynamicForm form)
			{
				String v = radioGroupItem.getValue().toString();
				if (v.equals("USA"))
					return true;
				else
					return false;
			}
		});

		serverFile = new TextItem(name + Constant.file_server_suffix);
		serverFile.setTitle(title);
		serverFile.setShowIfCondition(new FormItemIfFunction()
		{

			public boolean execute(FormItem item, Object value, DynamicForm form)
			{
				String v = radioGroupItem.getValue().toString();
				if (v.equals("Server File"))
					return true;
				else
					return false;
			}
		});

		FormItemIcon openIcon = new FormItemIcon();
		openIcon.setSrc("[SKIN]/TreeGrid/folder_open.png");
		serverFile.setIcons(openIcon);
		serverFile.addIconClickHandler(new IconClickHandler()
		{
			public void onIconClick(IconClickEvent event)
			{
				ServerFileWindow sfw = StaticHolder.getServerFileWindow();
				sfw.setTx(serverFile);
				sfw.show();

			}
		});
		serverFile.setAttribute("iconHSpace", ProgramForm.iconHspace);
		serverFile.setWidth(ProgramForm.CELL_WIDTH + ProgramForm.DEFAULT_ICON_WIDTH + ProgramForm.iconHspace);

		radioGroupItem.setShowTitle(false);
		radioGroupItem.setPrompt("Only one field can have value");
		ProgramForm.addInfoTooltip(radioGroupItem, param);
		radioGroupItem.setWidth(labelWidth + ProgramForm.CELL_WIDTH + ProgramForm.DEFAULT_ICON_WIDTH + ProgramForm.iconHspace);
		radioGroupItem.addChangedHandler(new ChangedHandler()
		{
			public void onChanged(ChangedEvent event)
			{
				redraw();
				
			}

		});
		if (bean.getValue(name + Constant.file_data_suffix) != null && bean.getValue(name + Constant.file_data_suffix).length() > 0)
		{

			fileData.setDefaultValue(bean.getValue(name + Constant.file_data_suffix));
			radioGroupItem.setValue("Text");
		} else if (bean.getValue(name + Constant.file_server_suffix) != null && bean.getValue(name + Constant.file_server_suffix).length() > 0)
		{
			serverFile.setDefaultValue(bean.getValue(name + Constant.file_server_suffix));
			radioGroupItem.setValue("Server File");
		} else if (bean.getValue(name + Constant.file_usa_suffix) != null && bean.getValue(name + Constant.file_usa_suffix).length() > 0)
		{
			USAItem.setDefaultValue(bean.getValue(name + Constant.file_usa_suffix));
			radioGroupItem.setValue("USA");
		} else
		{
			radioGroupItem.setValue("File");
		}

		if (mandatory)
		{
			// http://code.google.com/p/smartgwt/issues/detail?id=214
			// radioGroupItem.setValidators(new CustomValidator()
			// {
			// {
			// setErrorMessage("Value is required for entry below");
			// }
			//
			// @Override
			// protected boolean condition(Object value)
			// {
			// int numValue = 0;
			// if (ui.getValue() != null && ui.getValue().toString().length() >
			// 0)
			// numValue++;
			// if (fileData.getValue() != null &&
			// fileData.getValue().toString().length() > 0)
			// numValue++;
			// if (serverFile.getValue() != null &&
			// serverFile.getValue().toString().length() > 0)
			// numValue++;
			//
			// if (numValue == 0)
			// return false;
			// return true;
			// }
			// });

		}
	}
	
	public void redraw()
	{
		ui.redraw();
		fileData.redraw();
		serverFile.redraw();
		USAItem.redraw();
	}

}
